package org.example.model.Subscribe.Routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.example.util.ConnectionFactoryUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    // 定义交换机名称
    private final static String ROUTE_NAME = "exchange_routing";

    // 大促销路由 RoutingKey
    private final static String BIG_ROUT_KEY = "big";

    // 小促销路由 RoutingKey
    private final static String SMALL_ROUT_KEY = "small";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、建立工厂连接
        Connection connection = ConnectionFactoryUtil.getConnection();

        // 2、创建信道
        Channel channel = connection.createChannel();

        // 3、创建交换机
        /**
         * 参数1：交换机名
         * 参数2：交换机类型
         * 参数3：交换机持久化
         */
        channel.exchangeDeclare(ROUTE_NAME, BuiltinExchangeType.DIRECT,true);

        // 4、创建队列
        // 短信
        channel.queueDeclare("SEND_MESSAGE1", true, false, false, null);
        // 邮件
        channel.queueDeclare("SEND_MAIL1", true, false, false, null);
        // 站内信
        channel.queueDeclare("SEND_STATION1", true, false, false, null);

        // 5、交换机绑定队列
        /**
         * 参数1：队列名
         * 参数2：交换机名
         * 参数3：路由关键字，发布订阅模式写 ""即可
         */
        channel.queueBind("SEND_MAIL1", ROUTE_NAME, BIG_ROUT_KEY);

        channel.queueBind("SEND_MESSAGE1", ROUTE_NAME, BIG_ROUT_KEY);

        channel.queueBind("SEND_STATION1", ROUTE_NAME, BIG_ROUT_KEY);

        channel.queueBind("SEND_STATION1", ROUTE_NAME, SMALL_ROUT_KEY);

        // 6、发送消息
        channel.basicPublish(ROUTE_NAME, BIG_ROUT_KEY, null, ("尊敬的Vip用户，秒杀大商品开抢了！").getBytes());
        channel.basicPublish(ROUTE_NAME, SMALL_ROUT_KEY, null, ("尊敬的Vip用户，秒杀小商品开抢了！").getBytes());


        // 7、关闭资源
        channel.close();

        connection.close();
    }
}
